﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using DTO;


namespace BLL
{
    public class Mutation
    {
     
        #region Mutation for path representation
            private void inversionSegment(int p1, int p2, ref PathTour tour)
            {
                if (p1 <= p2)
                {
                    int tg = 0;
                    int i, j;
                    for (i = p1, j = p2; i <= (p1 + p2) / 2; i++, j--)
                    {
                        tg = tour.OrderCity[i];
                        tour.OrderCity[i] = tour.OrderCity[j];
                        tour.OrderCity[j] = tg;
                    }
                }
                else
                {
                    int tg = 0;
                    int i, j;
                    for (i = p2, j = p1; i <= (p1 + p2) / 2; i++, j--)
                    {
                        tg = tour.OrderCity[i];
                        tour.OrderCity[i] = tour.OrderCity[j];
                        tour.OrderCity[j] = tg;
                    }
                }
            }

            public void inversionMutation(ref PathTour tour, int numCity, double mutationRate, Random ran)
            {
                for (int i = 0; i < numCity; i++)
                {
                    if (ran.NextDouble() < mutationRate)
                    {
                        int pos1 = ran.Next(numCity);
                        int pos2 = ran.Next(numCity);
                        inversionSegment(pos1, pos2, ref tour);
                    }
                }
            }

            public void swapMutation(ref PathTour tour, int numOfCity, double mutationRate, Random ran)
            {
                int temp;
                for (int i = 0; i < numOfCity; i++)
                {
                    if (ran.NextDouble() < mutationRate)
                    {
                        int pos1 = ran.Next(numOfCity);
                        int pos2 = ran.Next(numOfCity);
                        temp = tour.OrderCity[pos1];
                        tour.OrderCity[pos1] = tour.OrderCity[pos2];
                        tour.OrderCity[pos2] = temp;                       
                    }
                }
            }


        #endregion
    }
}
